package com.rookie.im.codec;

import com.rookie.im.codec.proto.Message;
import com.rookie.im.codec.utils.MessageUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

import java.util.List;

public class WebSocketMessageDecoder extends MessageToMessageDecoder<BinaryWebSocketFrame> {

    @Override
    protected void decode(ChannelHandlerContext ctx, BinaryWebSocketFrame binaryWebSocketFrame, List<Object> list) throws Exception {
        ByteBuf in = binaryWebSocketFrame.content();
        Message message = MessageUtils.makeMessage(in);
        if (message == null) {
            list.add(binaryWebSocketFrame);
            return;
        }

        list.add(message);
    }
}
